import 'dart:io';
import 'package:base_lib/index.dart';
import 'package:base_lib/utils/log.dart';
import 'package:image_picker/image_picker.dart';
import 'package:doctor_inquiry_lib/constant/constant.dart';
import 'package:flutter/material.dart';
import 'circle_page_indicator.dart';
import 'extra_item.dart';

typedef void OnImageSelect(File mImg);

class DefaultExtraWidget extends StatefulWidget {
  final OnImageSelect onImageSelectBack;
  final Function onCallClick;
  final Function onVideoCallClick;
  final Function onExamineClick;
  final Function onRpClick;
  final Function onTransferClick;
  final Function onEndChatClick;

  const DefaultExtraWidget(
      {Key key,
      this.onImageSelectBack,
      this.onCallClick,
      this.onVideoCallClick,
      this.onExamineClick,
      this.onRpClick,
      this.onTransferClick,
      this.onEndChatClick})
      : super(key: key);

  @override
  _DefaultExtraWidgetState createState() => _DefaultExtraWidgetState();
}

class _DefaultExtraWidgetState extends State<DefaultExtraWidget> {
  final _currentPageNotifier = ValueNotifier<int>(0);

  var _pageController = new PageController(initialPage: 0);

  @override
  Widget build(BuildContext context) {
    return new Container(
        height: double.infinity,
        color: Color(0xffF6F6F6),
        child: Column(
          children: <Widget>[
            Expanded(
              child: Container(
                alignment: Alignment.center,
                child: new PageView.builder(
                  onPageChanged: (int index) {
                    _currentPageNotifier.value = index;
                  },
                  itemCount: 1,
                  controller: _pageController,
                  itemBuilder: (BuildContext context, int index) {
                    return getEveryPage(index);
                  },
                ),
              ),
            ),
            Container(
                child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: CirclePageIndicator(
                itemCount: 1,
                currentPageNotifier: _currentPageNotifier,
              ),
            ))
          ],
        ));
  }

  Widget getEveryPage(int index) {
    List<Widget> itemList = [
      createPicItem(),
      createShotItem(),
      createCallItem(),
      createVideoItem(),
      createExamineItem(),
      createRpItem(),
      createTransferItem(),
      createEndChatItem()
      // createFinishInfoItem(),
    ];

    List<Widget> itemList2 = [createEndChatItem()];

    return index == 0 ? getOnePage(itemList) : getOnePage(itemList2);
  }

  Widget getOnePage(List<Widget> itemList) {
    Widget page = Container(
      alignment: Alignment.center,
      child: GridView.count(
        //垂直子Widget之间间距
        mainAxisSpacing: 10.0,
        //GridView内边距
        padding: EdgeInsets.all(10.0),
        //一行的Widget数量
        crossAxisCount: 4,
        //子Widget宽高比例
        childAspectRatio: 1.0,
        //子Widget列表
        children: itemList.toList(),
      ),
    );
    return page;
  }

  ExtraItemContainer createPicItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/picture.png",
        text: "图片",
        onTab: () {
          Future<PickedFile> imageFile =
              ImagePicker().getImage(source: ImageSource.gallery);
          imageFile.then((result) {
            widget.onImageSelectBack?.call(File(result.path));
          });
        },
      );

  ExtraItemContainer createShotItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/shot.png",
        text: "拍照",
        onTab: () {
          Future<PickedFile> imageFile =
              ImagePicker().getImage(source: ImageSource.camera);
          imageFile.then((result) {
            widget.onImageSelectBack?.call(File(result.path));
          });
        },
      );

  ExtraItemContainer createCallItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/call.png",
        text: "电话问诊",
        onTab: widget.onCallClick,
      );

  ExtraItemContainer createVideoItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/video.png",
        text: "视频问诊",
        onTab: widget.onVideoCallClick,
      );

  ExtraItemContainer createExamineItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/examine.png",
        text: "检查检验",
        onTab: widget.onExamineClick,
      );

  ExtraItemContainer createRpItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/rp.png",
        text: "开处方",
        onTab: widget.onRpClick,
      );

  ExtraItemContainer createTransferItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/transfer.png",
        text: "转诊",
        onTab: widget.onTransferClick,
      );

  ExtraItemContainer createFinishInfoItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/finishInfo.png",
        text: "填写信息",
        onTab: () {
          FLog("填写信息");
        },
      );

  ExtraItemContainer createEndChatItem() => ExtraItemContainer(
        leadingIconPath: Constant.ASSETS_IMG_CHAT + "tools/end-chat.png",
        text: "结束问诊",
        onTab: widget.onEndChatClick,
      );

  ExtraItemContainer createEmptyItem() => ExtraItemContainer(
        leadingIconPath: '',
        leadingHighLightIconPath: '',
        text: "",
        onTab: () {
          FLog("占位createEmptyItem");
        },
      );
}
